package com.lly.songhigh.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lly.songhigh.R;
import com.lly.songhigh.entity.SingerSingle;
import com.lly.songhigh.utils.ChangeUtils;

import java.util.List;

/**
 * Created by 李新伟 on 15/11/5.
 */
public class SingerSingleListAdapter extends RecyclerView.Adapter<SingerSingleListAdapter.ViewHolder> {

    private Context context;
    private List<SingerSingle.DataEntity> dataEntities;
    private OnItemListener onItemListener;
    private OnMenuClickListener onMenuClickListener;

    public SingerSingleListAdapter(Context context, List<SingerSingle.DataEntity> dataEntities) {
        this.context = context;
        this.dataEntities = dataEntities;
    }

    public void refreshData(List<SingerSingle.DataEntity> dataEntities) {
        this.dataEntities = dataEntities;
        notifyDataSetChanged();
    }

    public void setOnItemListener(OnItemListener onItemListener) {
        this.onItemListener = onItemListener;
    }

    public void setOnMenuClickListener(OnMenuClickListener onMenuClickListener) {
        this.onMenuClickListener = onMenuClickListener;
    }

    public interface OnItemListener {
        void onItemClick(SingerSingle.DataEntity dataEntity, int position);
    }

    public interface OnMenuClickListener {
        void onMenuClick(SingerSingle.DataEntity dataEntity);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.charts_detail_item, null);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        SingerSingle.DataEntity dataEntity = dataEntities.get(position);
        String num = ChangeUtils.getCount(dataEntity.getFavorites());
        holder.songsListPosition.setText(position + 1 + "");
        holder.songsListSongName.setText(dataEntity.getName());
        holder.songsListItemSinger.setText(dataEntity.getSingerName());
        holder.songsListItemNumber.setText(num);
        holder.position = position;
    }

    @Override
    public int getItemCount() {
        return dataEntities != null && dataEntities.size() > 0 ? dataEntities.size() : 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder{

        private TextView songsListPosition;
        private TextView songsListSongName;
        private TextView songsListItemSinger;
        private TextView songsListItemNumber;
        private ImageView listMore;
        private LinearLayout songListItemLinear;
        private int position;

        public ViewHolder(View itemView) {
            super(itemView);
            songsListPosition = (TextView) itemView.findViewById(R.id.songs_list_position);
            songsListSongName = (TextView) itemView.findViewById(R.id.songs_list_songName);
            songsListItemSinger = (TextView) itemView.findViewById(R.id.songs_list_item_singer);
            songsListItemNumber = (TextView) itemView.findViewById(R.id.songs_list_item_number);
            listMore = (ImageView) itemView.findViewById(R.id.list_more);
            songListItemLinear = (LinearLayout) itemView.findViewById(R.id.detail_item_linear);

            listMore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onMenuClickListener != null) {
                        onMenuClickListener.onMenuClick(dataEntities.get(position));
                    }
                }
            });

            songListItemLinear.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemListener != null) {
                        onItemListener.onItemClick(dataEntities.get(position), position);
                    }
                }
            });
        }
    }
}
